package com.haotunet.app.goldtreasure.adapter;

import java.util.List;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.CouponInfo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DiscountAdapter extends HolderAdapter<CouponInfo, DiscountAdapter.ViewHolder> {

	public DiscountAdapter(Context context, List<CouponInfo> mlist) {
		super(context, mlist);

	}

	public static class ViewHolder {
		TextView tvValue;
		TextView tvValidityPeriod;
		TextView tvGetType;
		TextView tvUseCondition;
		TextView tvCouponNum;
		RelativeLayout couponLayout;
	}

	@Override
	public View buildConvertView(LayoutInflater layoutInflater, CouponInfo t, int position) {

		return inflate(R.layout.item_discount);
	}

	@Override
	public ViewHolder buildHolder(View convertView, CouponInfo t, int position) {
		ViewHolder holder = new ViewHolder();
		holder.tvValue = (TextView) convertView.findViewById(R.id.txt_coupon_value);
		holder.tvGetType = (TextView) convertView.findViewById(R.id.txt_coupon_get_way);
		holder.tvUseCondition = (TextView) convertView.findViewById(R.id.txt_use_condition);
		holder.tvValidityPeriod = (TextView) convertView.findViewById(R.id.txt_validity_period);
		holder.couponLayout = (RelativeLayout) convertView.findViewById(R.id.rl_coupon_bg);
		holder.tvCouponNum = (TextView) convertView.findViewById(R.id.coupon_num);
		return holder;
	}

	@Override
	public void bindViewDatas(View convertView, ViewHolder holder, CouponInfo couponInfo, int position) {
		holder.tvGetType.setText(couponInfo.getCouponType());
		holder.tvValue.setText(couponInfo.getValue());
		String couponsStatus = couponInfo.getCouponsStatus();
		if ("0".equals(couponsStatus)) {
			holder.couponLayout.setBackgroundResource(R.drawable.unused_coupon_bg);
		} else if ("1".equals(couponsStatus)) {
			holder.couponLayout.setBackgroundResource(R.drawable.used_coupon_bg);
		} else if ("2".equals(couponsStatus)) {
			holder.couponLayout.setBackgroundResource(R.drawable.expired_coupon_bg);
		}
		if (!"".equals(couponInfo.getCouponNum())) {
			holder.tvCouponNum.setVisibility(View.VISIBLE);
			holder.tvCouponNum.setText(couponInfo.getCouponNum());
		} else {
			holder.tvCouponNum.setVisibility(View.GONE);
		}
		holder.tvUseCondition.setText("." + couponInfo.getCondition());
		holder.tvValidityPeriod.setText(".有效期至 : " + couponInfo.getDeadline());
	}
}
